define([ 'jquery', 'jquery_route', 'arttemplateUtil' ,'common','config','pageJs','filer','bootstrapselect','validform','wdatePicker' ], function($,
		jquery_route, arttemplateUtil,common,config,pageJs,filer,bootstrapselect) {
	
	var coupon_type = 51;
	var pageNo = 1;
	var formValid;
	var act_price_conf_tpl;
	var chose_user_tpl;
	var user_list_tpl;
	var submit_enter_tpl;
	//下拉选择，切换不同
	function changeTypeOrGoods(){
		var valuenum = $("#coupon_type").children('option:selected').val();
		if(valuenum != 51){
			var goodsbox = $("#editForm select[name=goods_id]");//选择商品
			var sel = goodsbox.find("option:selected");
			var ts = sel.attr("data-type");
			var tp = sel.attr("data-price");
			if(ts == 1){//当选择关联销售商品才有此项配置，配置价格的按钮同时就隐藏。其中售价读取的是所选销售商品的会员价，优惠价设置需做判断，要小于售价
				$("#old_price").show();
				$("#new_price").show();
				formValid.unignore($("#goods_coupon_price"));
				
				$(".sj-name").html(tp);
				$("#price_conf").hide();
			}else if(ts == 2){
				$("#old_price").hide();
				$("#new_price").hide();
				formValid.ignore($("#goods_coupon_price"));
				
				$("#price_conf").show();
			}
			$("#goods_label_type").val(ts);
			
			$("#yhtj").hide();      //优惠条件
	        $("#yhnr").hide();      //优惠内容
	        formValid.ignore($("#coupon_condition"));
	        formValid.ignore($("#reduce_price"));
	        
			if (valuenum == 53) {
		        $("#qymc").hide();      //企业名称
		        formValid.ignore($("#enter_name"));
		        
		        $("#yhqsl").hide();    //优惠券数量
		        formValid.ignore($("#total_num"));
		        
		        $("#fffw").show();      //分发范围
		    } else if (valuenum == 52) {
		        $("#fffw").hide();      //分发范围
		        
		        $("#qymc").show();      //企业名称
		        formValid.unignore($("#enter_name"));
		        
		        $("#yhqsl").show();    //优惠券数量
		        formValid.unignore($("#total_num"));
		    }
		}else{
			$("#yhtj").show();      //优惠条件
	        $("#yhnr").show();      //优惠内容
	        formValid.unignore($("#coupon_condition"));
	        formValid.unignore($("#reduce_price"));
	        
	        $("#fffw").show();      //分发范围
	        
	        $("#old_price").hide();
			$("#new_price").hide();
			formValid.ignore($("#goods_coupon_price"));
			
	        $("#price_conf").hide();//配置价格
	        //formValid.ignore($("#goods_act_prices"));
	        
	        $("#qymc").hide();      //企业名称
	        formValid.ignore($("#enter_name"));
	        
	        $("#yhqsl").hide();    //优惠券数量
	        formValid.ignore($("#total_num"));
		}
	}
	
	function initTpl(){
		act_price_conf_tpl = arttemplateUtil.template.compile($.setHTML('market/templates/activity_price_conf_tpl.html'));
		chose_user_tpl = arttemplateUtil.template.compile($.setHTML('market/templates/coupon_user_tpl.html'));
		user_list_tpl = arttemplateUtil.template.compile($.setHTML('market/templates/coupon_user_list_tpl.html'));
		submit_enter_tpl = arttemplateUtil.template.compile($.setHTML('market/templates/act_coupon_submit_enter_tpl.html'));
	}
	function saveForm(){
		var coupon_type = $("#coupon_type").children('option:selected').val();
		var obj = {};
		if($("#act_coupon_id").val() != ''){
			obj.id=$("#act_coupon_id").val();
		} else {
			pageNo = 1;
		}
		obj.coupon_activity_type = coupon_type;
		obj.coupon_name = $("#act_name").val();
		obj.coupon_num = $("#total_num").val();
		obj.goods_id = $("#editForm select[name=goods_id]").children('option:selected').val();
		obj.goods_coupon_price = $("#goods_coupon_price").val();
		obj.favorable_conditions = $("#coupon_condition").val();
		obj.favorable_content=$("#reduce_price").val();
		obj.first_effective_date = $("#valid_begin_time").val();
		obj.end_effective_date = $("#valid_end_time").val();
		obj.favorable_tip = $("#remark").val();
		obj.enterprise_name = $("#enter_name").val();
		obj.distribution_scope = $("input[name='user_range']:checked").val();
		obj.goodsSKuListJson = $("#goods_act_prices").val();
		obj.user_id_arr = $("#coupon_user_list").val();
		
		common.loading();
		common.common_ajax_request('/api/seller/v1/saveOrUpdateActivityCoupon',obj,false,function(data,code,msg){
			if(code==200 && msg.businessCode==0){
        		window.layer.alert('保存成功!', {
      			  icon: 1,closeBtn:false
      			}, function(){
      				$('#submit_enter_modal').modal('hide');
      				//刷新当前页面
      				window.location.href="#market/activitycoupon/" + coupon_type + "/" +pageNo;
      			}); 
			}else{
				layer.alert(msg.businessNote);
			}
			layer.closeAll('loading');
		}, null, "post");
	}
	function initBtn(){
		/*$("#save_btn").click(function(){
			if(formValid.check()){
				var coupon_type = $("#coupon_type").children('option:selected').val();
				
				var obj = {};
				obj.coupon_activity_type = coupon_type;
				obj.coupon_activity_text = $("#coupon_type").children('option:selected').text();
				
				obj.coupon_name = $("#act_name").val();
				obj.coupon_num = $("#total_num").val();
				obj.goods_name = $("#editForm select[name=goods_id]").children('option:selected').text();
				obj.goods_coupon_price = $("#goods_coupon_price").val();
				obj.goods_label_type = $("#goods_label_type").val();
				obj.favorable_conditions = $("#coupon_condition").val();
				obj.favorable_content=$("#reduce_price").val();
				obj.first_effective_date = $("#valid_begin_time").val();
				obj.end_effective_date = $("#valid_end_time").val();
				obj.favorable_tip = $("#remark").val();
				obj.enterprise_name = $("#enter_name").val();
				obj.distribution_scope = $("input[name='user_range']:checked").val();
				console.info($("#goods_act_prices").val());
				if($("#goods_act_prices").val() != ''){
					obj.goodsSKuList = $.parseJSON($("#goods_act_prices").val());
				}
				obj.user_account = $(".personbox").html().replace(/<[^>]+>/g,"");
				
				//加入确认提交内容框
				$('#submit_enter_modal').modal('show');
				var res = {
					data:obj
				};
    			$("#submitContent").html(submit_enter_tpl(res));
			}
		});*/
		$("#save_btn").click(function(){
			if(formValid.check()){
					var msg = "请认真核对信息是否配置正确";
					if($("#act_coupon_id").val() == '' && 
							$("#coupon_type").children('option:selected').val()!=52 && 
							($("input[name='user_range']:checked").val() == 1 || $("input[name='user_range']:checked").val() == 2)){
						if($("input[name='user_range']:checked").val() == 1){
							msg  += "，确认提交后将会给全部用户进行消息推送通知。";
						}else{
							msg  += "，确认提交后将会给指定的【"+$("#coupon_user_list").val().split(",").length+"】个用户进行消息推送通知。";
						}
						
					}
					layer.confirm('<span class="redstar">'+msg+'</span>', {
						  btn: ['确定','取消'] //按钮
						}, function(){
							saveForm();
						}, function(){
						  
					});
			}
			return;
		});
		$("#ret_btn").click(function(){
			window.location.href="#market/activitycoupon/" + coupon_type + "/" +pageNo;
		});
	}
	
	function initApcBtn(){
		//重置
		$("#apc_reset_btn").click(function(){
			var prices = $(".goods_act_price");
			$.each(prices,function(index,data){
				$(prices[index]).val('');
			});
		});
		//提交
		$('#apc_submit_btn').off('click').on('click',function(){
			var prices = $(".goods_act_price");
			var reg = /^0\.[1-9]$|^0\.[1-9][0-9]$|^0\.[0-9][1-9]$|^([1-9][0-9]{0,3})(\.[0-9]{1,2})?$/;
			
			var act_goods_price_arr = [];
			var flag = true;
			var gsFlag = true;
			var bsFlag = true;
			var act_price_null_indexs = [];
			var act_price_err_indexs = [];
			$.each(prices,function(index,data){//
				if($(prices[index]).val()==''){
					act_price_null_indexs.push(index+1);
					flag=false;
				}else{
					var pric = $(prices[index]);
					var gp = {};
					gp.skuId = pric.attr("data-id");
					gp.skuName = pric.attr("data-skuname");
					gp.deleveryTime = pric.attr("data-deleverytime");
					var gpbPrice = pric.attr("data-gpb");
					gp.gpbPrice = pric.attr("data-gpb");
					gp.goodsActivityPrice = pric.val();
					act_goods_price_arr.push(gp);
					
					if(!reg.test(pric.val())){
						act_price_err_indexs.push(index+1);
						gsFlag = false;
					}else if(parseFloat(gpbPrice) <= parseFloat(pric.val())){
						act_price_err_indexs.push(index+1);
						bsFlag = false;
					}
				}
			});
			if(!flag){
				layer.msg("第【"+act_price_null_indexs.join(",")+"】个优惠价格不能为空！");
			}else if(!gsFlag){
				layer.msg("第【"+act_price_err_indexs.join(",")+"】个请输入0.01~9999.99的数字！");
			}else if(!bsFlag){
				layer.msg("第【"+act_price_err_indexs.join(",")+"】个优惠价不能大于或等于售价！");
			}else{
				$("#goods_act_prices").val(JSON.stringify(act_goods_price_arr));
				$('.coupon_price').modal('hide');
				if(formValid.check(true, $("#goods_id"))){
					$("#editForm #goods_id").removeClass("Validform_error");
					$("#editForm #goods_act_prices").next().removeClass("Validform_wrong");
					$("#editForm #goods_act_prices").next().empty();
				}
			}
		});
	}
	
	function getInitData(id){
		var obj = {};
		common.ajax_request2('post','/api/seller/v1/queryActivityCouponDetail',{coupon_activity_id:id},function($data,$code){
			if($code==200){
				obj.id = $data.id;
				obj.coupon_type = $data.coupon_type;
				obj.act_name = $data.act_name;
				obj.total_num = $data.total_num;
				obj.goods_label_type = $data.goods_label_type;
				obj.goods_id = $data.goods_id;
				var arr = $data.coupon_goods_price_list == '' ?[]:$data.coupon_goods_price_list;
				var gpArr = [];
				for(var i=0; i<arr.length; i++){
					var gp = {};
					gp.skuId = arr[i].goods_sku_id;
					gp.goodsActivityPrice = arr[i].goods_coupon_price;
					gpArr.push(gp);
				}
				if(obj.goods_label_type == 1){//判断如果是销售商品
					obj.goods_coupon_price = gpArr.length > 0?gpArr[0].goodsActivityPrice:'';
				}else if(obj.goods_label_type == 2){
					obj.goods_act_prices = JSON.stringify(gpArr);
				}
				obj.coupon_condition = $data.coupon_condition;
				obj.reduce_price = $data.reduce_price;
				obj.valid_begin_time = $data.valid_begin_time;
				obj.valid_end_time = $data.valid_end_time;
				obj.remark = $data.remark;
				obj.user_range = $data.user_range;
				obj.enter_name = $data.enter_name;
				var userArr = $data.coupon_user_list;
				var userIdArr = [];
				var userAccountArr = [];
				for(var i=0; i<userArr.length; i++){
					userIdArr.push(userArr[i].user_id);
					userAccountArr.push(userArr[i].user_account+"&nbsp;&nbsp;&nbsp;&nbsp;<a class='delUserAccountBtn' data-id='"+userArr[i].user_id+"' data-account='"+userArr[i].user_account+"' href='javascript:void(0);' style='color:red;'>X</a>");
				}
				obj.coupon_user_list = userIdArr.join(",");
				obj.personbox = userAccountArr.join("<br>");
				initDelUserBtn();
			}
		}, false);
		return obj;
	}
	function initDelUserBtn(){
		$(document).off("click",".delUserAccountBtn").on("click",".delUserAccountBtn",function(){
			var delId = $(this).attr("data-id");
			var delAccount = $(this).attr("data-account");
			var delIndex = -1;
			var userArr = $("#coupon_user_list").val().split(",");
			var accountArr = $(".personbox").html().split("<br>");
			delIndex = userArr.indexOf(delId);
			if(delIndex != -1){
				userArr.splice(delIndex, 1);
				accountArr.splice(delIndex, 1);
				$("#coupon_user_list").val(userArr.join(","));
				$(".personbox").html(accountArr.join("<br>"));
			}
			initDelUserBtn();
		});
	}
	function getSkuPrice(skuId){
		var actPrice = $("#goods_act_prices");
		var skuPrices = [];
		if(actPrice.val() != ''){
			skuPrices = $.parseJSON(actPrice.val());
		}
		var retVal = '';
		for(var i = 0; i<skuPrices.length; i++){
			if(skuPrices[i].skuId == skuId){
				retVal = skuPrices[i].goodsActivityPrice;
				continue;
			}
		}
		return retVal;
	}
	
	var user_couponApp = {
		param:{pageNo:1, pageSize:1000},
		name:'指定用户相关操作',
		//用户列表分页
		pagefun:function(page){
			//分页回调
			user_couponApp.param.pageNo=page
			user_couponApp.initUserDetail();
		},
		initUserDetail : function() {
			//加入查询条件
			$.each($('#user_coupon_content form.search_userfrm').serializeArray(), function(i, item) {
				if(item.value==""){
						delete user_couponApp.param[item.name]
					}else{
						user_couponApp.param[item.name]=item.value;
					}
			});
			
			user_couponApp.param.is_old_user=$('#user_coupon_content #oldmen').is(':checked');
			common.ajax_request2('get','/api/seller/v1/queryActivityCouponUserList',user_couponApp.param,function($data,$code){
				if($code==200){
					if($data.totalCount > 0) {
						var resultObj = {};
						var userArr = $("#coupon_user_list").val()==''?[]:($("#coupon_user_list").val()).split(",");
						resultObj.data = $data;
						$('.tablelist').html(user_list_tpl(resultObj));
						$('input[name="userCheck"]').each(function(){
							if(userArr.indexOf($(this).val())!=-1){
								$(this).prop("checked",true);
							} 
						});
						pageJs.setPage_dynamic(
								{pageBox:'#view_pageBox',records:'#view_records',page:'#view_page'},
								$data.pageNo,$data.totalPage,$data.totalCount,user_couponApp.pagefun);
						initTableCheckBox();
					}else{
						$('.tablelist').html(user_list_tpl({}));
					}
				}
			},true);
		}
	};
	
	function initTableCheckBox(){
		$("#checkAllBtn").click(function(){
			if($(this).is(':checked')){
				$("input[name='userCheck']").prop("checked",true); 
			}else{
				$("input[name='userCheck']").prop("checked",false); 
			}
		});
		$("#checkUserBtn").click(function(){
			var chk_value =[];
			var chk_account = [];
			$('input[name="userCheck"]:checked').each(function(){
				chk_value.push($(this).val()); 
				chk_account.push($(this).attr("data-account"));
			}); 
			if(chk_value.length == 0){
				layer.alert('你还没有选择任何内容！');
			}else{
				var userArr = $("#coupon_user_list").val()==''?[]:($("#coupon_user_list").val()).split(",");
				var accountArr = $(".personbox").html() == ''?[]:($(".personbox").html()).split("<br>");
				for(var i = 0; i<chk_value.length; i++){
					if(userArr.indexOf(chk_value[i]) == -1){
						userArr.push(chk_value[i]);
						accountArr.push(chk_account[i]+"&nbsp;&nbsp;&nbsp;&nbsp;<a class='delUserAccountBtn' data-id='"+chk_value[i]+"' data-account='"+chk_account[i]+"' href='javascript:void(0);' style='color:red;'>X</a>");
					}
				}
				$("#coupon_user_list").val(userArr.join(","));
				$(".personbox").html(accountArr.join("<br>"));
				$('#user_modal').modal('hide');
				initDelUserBtn();
			}
		});
	}
	
	function initPage(){
		//下拉选择，切换不同
		changeTypeOrGoods();
        $("#coupon_type").change(function() {
        	changeTypeOrGoods();
        });
        //radio分发范围切换用户
        $("#fffw1").click(function() {
            $(".addperson").hide();
            $("#editForm #chk_personbox").removeClass("Validform_error");
			$("#editForm #chk_personbox").next().removeClass("Validform_wrong");
			$("#editForm #chk_personbox").next().empty();
        });
        $("#fffw2").click(function() {
            $(".addperson").show();
        });
        $("#fffw3").click(function() {
            $(".addperson").hide();
            $("#editForm #chk_personbox").removeClass("Validform_error");
			$("#editForm #chk_personbox").next().removeClass("Validform_wrong");
			$("#editForm #chk_personbox").next().empty();
        });
        
        initTpl();
        
        initBtn();
        
        //价格配置
    	$(document).off('click','#price_conf').on('click','#price_conf',function(){
    		var goodsId = $("#editForm select[name=goods_id]").val();
    		if(goodsId != '' && goodsId != 0){
    			common.loading();
        		$('.coupon_price').modal('show');
        		common.common_ajax_request('/api/v1/querySKUList',{goodsId:goodsId},false,function(data,code,msg){
        			var skuArr = [];
        			for(var i=0; i<data.length; i++){
        				data[i].sku_act_price = getSkuPrice(data[i].id);
        			}
        			var res = {
    					records:data
    				};
        			$("#coupon_content").html(act_price_conf_tpl(res));
        			initApcBtn();
        			layer.closeAll('loading');
        		}, null, "post");
    		}else{
    			layer.alert("请先选择商品！");
    		}
    	});
    	
    	
    	//地区搜索功能
		var selectcomm={
				searchflag:true,
				tmp_searchbox_input_val:'',
				queryfzAreaURL:'/api/seller/v1/queryfzArea',
				queryCommunityByAreaIdURL:'/api/v1/queryCommunityByAreaId',
				init:function(objid){
					var _selectcommobj=$(objid);
					//查询福州地区的小区
					_selectcommobj.selectpicker({
					      //liveSearch: true,
					      //maxOptions: 1
					 });
					var fzAreaObj={};
					var fzAreaFirst={};
					common.ajax_request2('get',selectcomm.queryfzAreaURL,{},function($data,$code){
						if($code==200){
							if($data.length>0){
								fzAreaFirst=$data[0];
							}
							$.each($data,function(_index,_data){
								fzAreaObj[_data.id]=_data;
							});
							var _searchbox_input=_selectcommobj.parent().find('.dropdown-menu>.bs-searchbox>input');
							var commLoad=function($stxt){
								if(typeof $stxt =='undefined'){
									_searchbox_input.val('');
								}
								//如果内容为空且已经执行过areaid查询则不做任何操作
								if($stxt==''&&selectcomm.searchflag){
									return;
								}
								var _area_id=fzAreaFirst.id;
								if(_area_id!=''){
									_searchbox_input;
									var _param={area_id:_area_id};
									if(typeof $stxt != 'undefined' && $stxt!=''){
										//目前做area_name查询
										selectcomm.searchflag=false;
										_param.area_id=fzAreaFirst.parent_id;
										_param.area_name=$stxt;
									}else{
										//目前做areaid查询
										selectcomm.searchflag=true;
									}
									common.ajax_request2('POST',selectcomm.queryCommunityByAreaIdURL,_param,function($data,$code){
										if($code==200){
											_selectcommobj.empty();
											_selectcommobj.append('<option value>全部小区</option>');
											$.each($data,function(index,data){
												_selectcommobj.append('<option value="'+data.id+'" data-pid="'+data.pid+'"  data-subtext="'+fzAreaObj[data.pid].areaName+'">'+data.community_name+'</option>');
											});
//											var  _comm_id=$.cookie('proxyorder_comm_id');
//											if(typeof _comm_id !='undefined'){
//												_selectcommobj.find('option[value="'+_comm_id+'"]').attr("selected",true);
//											}
											// 刷新搜索功能
											_selectcommobj.selectpicker('refresh');
										}
									});
								}else{
									_selectcommobj.empty();
									_selectcommobj.append('<option value>全部小区</option>');
									_selectcommobj.selectpicker('refresh');
								}
							};
							_searchbox_input.keyup(function(e){
								if(selectcomm.tmp_searchbox_input_val!=$.trim($(this).val())){
									selectcomm.tmp_searchbox_input_val=$.trim($(this).val());
									commLoad(selectcomm.tmp_searchbox_input_val);
								}
								
							});
							commLoad();
						}
					});
				}
		};
		
    	//选择指定用户按钮
    	$(document).off('click','#chose_user_btn').on('click','#chose_user_btn',function(){
			common.loading();
			var _userobj={};
			common.ajax_request2('post','/api/seller/v1/queryServiceGoods',{label_type:"2"},function($data,$code){
				if($code==200){
					_userobj.goodslist=$data;
					$('#user_modal').modal('show');
					$('#user_coupon_content').html(chose_user_tpl(_userobj));
					selectcomm.init('.selectcomm');
				}
	    		layer.closeAll('loading');
			});
    	});
    	//商品分类选项
    	$(document).off('change','#user_coupon_content select.label_type').on('change','#user_coupon_content select.label_type',function(){
    		var _goodsid=$('#user_coupon_content select.goodsid');
    		_goodsid.empty();
    		_goodsid.append('<option value="">全部</option>');
    		common.loading();
			common.ajax_request2('post','/api/seller/v1/queryServiceGoods',{label_type:$(this).val()},function($data,$code){
				if($code==200){
					$.each($data.records,function(_index,_data){
						_goodsid.append('<option value="'+_data.id+'">'+_data.goods_name+'</option>');
					});
				}
	    		layer.closeAll('loading');
			});
    	});
    	//查询用户
    	$(document).off('click','.search_user_btn').on('click','.search_user_btn',function(){
    		user_couponApp.param.pageNo=1;
    		user_couponApp.initUserDetail();
    	});
	}
	

	
	$.controller("controller.market.activitycoupon_edit", function($model, $tplHtml, $defobj,
			$modelobj, $paramobj, $this) {
		coupon_type = $paramobj.coupon_type;
		pageNo = $paramobj.page;
		
		$defobj.data = {};
		$goods_id = '';
		
		if($paramobj.id==''){
			$defobj.title="新增优惠券";
		}else{
			$defobj.title="编辑优惠券";
			$defobj.data = getInitData($paramobj.id);
			$goods_id = $defobj.data.goods_id;
		}
		$defobj.data.curDate = new Date().getTime();
		
		$model($defobj);//传至到页面
		
		//设置指定用户信息
		$(".personbox").html($defobj.data.personbox);
		
		formValid = $("#editForm").Validform({
			tiptype:4,
			postonce:true,
			isShowRight:false,
			ignoreHidden:true,
			datatype:{
				"dateCompare":function(gets,obj,curform,regxp){
					
					if($("#editForm #valid_begin_time").val()=="" || $("#editForm #valid_end_time").val()==""){
						return "使用有效期不能为空";
					}else{
						var regEx = new RegExp("\\-","gi"); 
						var beginT = Date.parse($("#editForm #valid_begin_time").val().replace(regEx,"/"));
						var endT = Date.parse($("#editForm #valid_end_time").val().replace(regEx,"/"));
						var currentT = new Date().getTime();
						
						if(beginT > endT){
							return "使用有效期开始时间不能大于结束时间";
						}else if((beginT - currentT)<(90*60*1000)){//开始时间必须在当前时间1个小时之后
							return "开始时间必须在当前时间1.5个小时之后";
						}else if((endT - beginT) < (12*60*60*1000)){//两个时间相隔不能小于12小时
							return "两个时间相隔不能小于12小时";
						}
					}
					return true;
				},"coupon_price_check":function(gets,obj,curform,regxp){
					var reg = /^0\.[1-9]$|^0\.[1-9][0-9]$|^0\.[0-9][1-9]$|^([1-9][0-9]{0,3})(\.[0-9]{1,2})?$/;
					obj.attr('errormsg','输入0.01~9999.99');
					var oldPrice = parseFloat($(".sj-name").html());
					if(!reg.test(obj.val())){
						obj.attr('errormsg','输入0.01~9999.99');
						return false;
					}
					if(parseFloat(obj.val()) >= oldPrice){
						obj.attr('errormsg','优惠价格不能大于等于原价！');
						return false;
					}
					return true;
				},"priceCompare": function(gets,obj,curform,regxp){ 
					var reduce_price = $("#reduce_price").val(); //减免价格
					var condition_price = $("#coupon_condition").val(); //消费价格
					var reg = /^0\.[1-9]$|^0\.[1-9][0-9]$|^0\.[0-9][1-9]$|^([1-9][0-9]{0,3})(\.[0-9]{1,2})?$/;
					
					if(obj.val()=='' || !reg.test(obj.val())) {
						obj.attr('errormsg','输入0.01~9999.99');
						return false;
					}
					
					if(reduce_price!='' && condition_price!='') {
						if (parseFloat(condition_price) <= parseFloat(reduce_price)) {
							return "消费价格不能小于或等于减免价格";
						}
					}
					$("#reduce_price").removeClass("Validform_error").next().removeClass("Validform_wrong").html("");
					$("#coupon_condition").removeClass("Validform_error").next().removeClass("Validform_wrong").html("");
					return true;
				}, "selgoods":function(gets,obj,curform,regxp){
					if(obj.val() == '') {
						return false;
					}
					var gap = $("#goods_act_prices");
					//判断价格按钮是否隐藏状态price_conf
					if(!$("#price_conf").is(':hidden') &&gap.val()==''){
						obj.attr('errormsg','配置价格不能为空');
						return false;
					}else if($("#goods_id").val() == ''){
						obj.attr('nullmsg',"请选择关联商品");
						return false;
					}
					return true;
				}, "chk_personbox":function(gets,obj,curform,regxp){
					$("#chk_personbox").val(Math.random());
					var a = $("input[name='user_range']:checked").val();
					if(a == 2){
						if($("#coupon_user_list").val() == ''){
							return false;
						}
					}
					return true;
				}
			},
		});
		
		//设置关联商品
		var goodsbox = $("#editForm select[name=goods_id]");
		common.ajax_request2('post','/api/seller/v1/queryCouponGoodsList',{},function($data,$code){
			if($code==200){
				goodsbox.empty();
				goodsbox.append('<option value>请选择</option>');
				$.each($data.records,function(index,data){
					if($goods_id !=""){
						if($goods_id==data.id){
							goodsbox.append('<option value="'+data.id+'" data-type="'+data.label_type+'" data-price="'+data.goods_current_price+'" selected=true >'+data.goods_name+'</option>');
						}else{
							goodsbox.append('<option value="'+data.id+'" data-type="'+data.label_type+'" data-price="'+data.goods_current_price+'" >'+data.goods_name+'</option>');
						}
					}else{
						goodsbox.append('<option value="'+data.id+'" data-type="'+data.label_type+'" data-price="'+data.goods_current_price+'" >'+data.goods_name+'</option>');
					}
				});
			}
		},false);
		
		goodsbox.change(function(){
			$("#goods_act_prices").val("");
			changeTypeOrGoods();
		});
		
		initPage();
	});

	var App={
			name:''
	};
});